package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.AllocationOutDocumentsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.AllocationOutMaterialsNoSearchActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationDocumentsBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * 调拨单据列表
 *
 * @author 詹建宇
 * @time on 2018/3/29 16:40
 * @email jianyu.zhan@inossem.com
 */
public class AllocationDocumentsAdapter extends BaseAdapter {

    private Activity activity;
    private String mCondition;
    private List<AllocationDocumentsBean.DocumentBodyBean> list;
    private AllocationOutDocumentsActivity.SearchListener listener;

    public AllocationDocumentsAdapter(Activity activity, String mCondition, List<AllocationDocumentsBean.DocumentBodyBean> list, AllocationOutDocumentsActivity.SearchListener listener) {
        this.activity = activity;
        this.mCondition = mCondition;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.cy_view_out_storage_allocation_inorder_first, null);
            EditText condition = (EditText) convertView.findViewById(R.id.keyword);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition);
            condition.setHint("请输入调拨单号");
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(activity, condition);
                    listener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(activity, v);
                    listener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            AllocationDocumentsBean.DocumentBodyBean bean = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(activity).inflate(R.layout.cy_view_out_storage_allocating_havedocument_item, null);
                viewHolder = new ViewHolder();
                viewHolder.codeTv = (TextView) convertView.findViewById(R.id.codeTv);
                viewHolder.placeTv = (TextView) convertView.findViewById(R.id.placeTv);
                viewHolder.timeTv = (TextView) convertView.findViewById(R.id.timeTv);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            initData(activity, viewHolder, convertView, bean);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView codeTv, placeTv, timeTv;
    }

    private void initData(Activity context, ViewHolder viewHolder, View view, AllocationDocumentsBean.DocumentBodyBean bean) {
        viewHolder.codeTv.setText(bean.getRefer_receipt_code());
        viewHolder.placeTv.setText("接收库存地点：" + bean.getLocation_name_input());
        viewHolder.timeTv.setText(bean.getDemand_date());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, AllocationOutMaterialsNoSearchActivity.class);
                intent.putExtra(AllocationOutStorageExtra.ID, bean.getRefer_receipt_id());
                intent.putExtra(AllocationOutStorageExtra.ALLOCATION_TYPE, "0");
                context.startActivity(intent);
            }
        });
    }
}
